package com.culture.bootdemo.controller;

import com.culture.bootdemo.model.dto.MenuDTO;
import com.culture.bootdemo.utils.ListSelfUtil;
import com.culture.bootdemo.utils.MenuUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @Author: wenjl
 * @Description:
 * @Date: 2021/2/4 14:41
 */
@RestController
@RequestMapping("/collection")
public class CollectionController {


    /**
     * 自定义大list拆分工具类：ListSelfUtil.averageAssign
     * 开源大list拆分工具：ListUtils.partition
     * 线程安全集合类：map set list
     */
    @GetMapping("test1")
    public void test1() {
        //应用场景：多线程下运行，假设有8个线程，则分成8个小list。
        int limit = 8;
        List<String> listData = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
        System.out.println(listData.size());
        System.out.println(listData.size() % limit == 0 ? listData.size() / limit : listData.size() / limit + 1);
        //用途;  将大list切分成小list，可指定每个小list的条数
        List<List<String>> partition = ListUtils.partition(listData, 3);
        System.out.println(partition);

        //=============================================================================

        /**
         * 将大list拆分成小list，按size拆分，最后一个list不够size的话，合并到前一个list
         */
        List<List<String>> lists = ListSelfUtil.averageAssign(listData, 3);
        System.out.println("===:" + lists);


        //======================线程安全集合类  start =================================
        Map<Object, Object> map = Maps.newConcurrentMap();
        Set<Object> set = Sets.newConcurrentHashSet();
        List<Object> objects = Lists.newCopyOnWriteArrayList();
        List<Object> list = Collections.synchronizedList(new ArrayList<>());

        //======================线程安全集合类  end =================================

        int size = 100;
        //已知大小的map  指定初始化容量
        Map<Object, Object> objectObjectHashMap = Maps.newHashMapWithExpectedSize(size);

    }

    /**
     * ImmutablePair key和value不可变
     * MutablePair  可变
     * 键值
     */
    @GetMapping("test2")
    public void test2() {
        //ImmutablePair key和value不可变
        Pair<String, Object> pair = new ImmutablePair<>("1", "Two");
        //或者写法为：
        //Pair<String, Object> pair;
        //pair=new ImmutablePair<>("1","one");
        System.out.println(pair.getLeft());
        System.out.println(pair.getRight());

        //MutablePair  可变
        MutablePair<String, Object> mutablePair = new MutablePair<>();
        mutablePair.setLeft("2");
        mutablePair.setRight("Two");
        mutablePair.setLeft("3");
        mutablePair.setRight("Three");
        System.out.println(mutablePair.getLeft());
        System.out.println(mutablePair.getRight());
    }

    /**
     * MultiValueMap: 同一个key，存多个value
     */
    @GetMapping("test3")
    public void test3() {
        MultiValueMap map = new MultiValueMap();
        map.put("111", "test1");
        map.put("111", "test2");
        map.put("111", "test3");

        System.out.println(map);
        Collection collection = map.getCollection("111");
        List<String> arrayList = new ArrayList<>(collection);
        System.out.println(arrayList);
    }


    /**
     * 两个集合做差集，求A-B的结果
     */
    @GetMapping("test4")
    public void test4() {
        List<Integer> list1 = Lists.newArrayList(1, 2, 3, 4,88);
        List<Integer> list2 = Lists.newArrayList(3, 0, -1, -3, 2, 54, 324, 33, 76, 1, 4);
        //list2-list1结果集
        List<Integer> subtract = (List<Integer>)CollectionUtils.subtract(list2, list1);

        //list1中任意一个元素 在list2中
        boolean a = CollectionUtils.containsAny(list2, list1);
        //list2全部包含list1
        boolean b = CollectionUtils.containsAll(list2, list1);
    }

    /**
     * 获取树形结构的菜单
     */
    @GetMapping("testListTOTree")
    public void testListTOTree() {
        List<MenuDTO> list = new ArrayList<>();
        MenuDTO menu1 = new MenuDTO(1, "菜单一", 0);
        MenuDTO menu2 = new MenuDTO(2, "菜单二", 0);
        MenuDTO menu3 = new MenuDTO(3, "菜单三", 0);
        MenuDTO menu4 = new MenuDTO(4, "菜单四", 3);
        list.add(menu1);
        list.add(menu2);
        list.add(menu3);
        list.add(menu4);

        List<MenuDTO> menuDTOS = MenuUtil.streamToTree(list, 0);
        System.out.println(menuDTOS.toString());
    }

    /**
     * 多个字符串校验空
     *
     * @param css
     * @return
     */
    public static boolean isValid(final CharSequence... css) {
        return StringUtils.isNoneBlank(css);
    }


    public static void main(String[] args) {
        List<Integer> list1 = Lists.newArrayList(1, 2, 3, 4,88);
        List<Integer> list2 = Lists.newArrayList(3, 0, -1, -3, 2, 54, 324, 33, 76, 1, 4);

        boolean a = CollectionUtils.containsAny(list2, list1);
        boolean b = CollectionUtils.containsAny(list1, list2);
        List<Integer> subtract = (List<Integer>)CollectionUtils.subtract(list2, list1);
        System.out.println(a);
        System.out.println(b);
        System.out.println(subtract);
    }


}
